﻿using Common.Result;
using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.Orders;
using StackExchange.Redis;

namespace TakeawayWebApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly IOrderService _orderService;
        private readonly ILogger<OrderController> _logger;
        public OrderController(IOrderService orderService, ILogger<OrderController> logger)
        {
            _orderService = orderService;
            _logger = logger;
        }
        /// <summary>
        /// 提交订单
        /// </summary>
        /// <param name="orderAdd"></param>
        /// <returns></returns>
        [HttpPost("submit")]
        public async Task<ApiResult> Submit(OrderAdd orderAdd)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            orderAdd.UserId = userId;
            await _orderService.SubmitAsync(orderAdd, HttpContext);
            return ResultHelper.Success("下单成功");
        }

        [HttpGet]
        [Route("userPage")]
        public async Task<ApiResult> GetUserOrders(int page,int pageSize)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            return ResultHelper.Success(await _orderService.GetUserOrdersAsync(page, pageSize, userId));
        }

        [HttpGet]
        [Route("page")]
        public async Task<ApiResult> GetOrders(int page, int pageSize, string? number, DateTime? beginTime, DateTime? endTime)
        {
            return ResultHelper.Success(await _orderService.GetOrdersAsync(page, pageSize, number, beginTime, endTime));
        }
        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="orderEdit"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> EditOrdersStatusById(OrderEdit orderEdit)
        {
            await _orderService.EditOrdersStatusByIdAsync(orderEdit);
            return ResultHelper.Success("修改成功");
        }
    }
}
